function [ W, decibels_max, r_optim ] = embedwmark( Image,msg,rmin, rmax, m)
%EMBEDWMARK Implementation of a PRND number of lenght D with seed S in
%picture L using the implementation factor m in sector rmin-rmax
%   [ W ] = embedwmark( slika,D,S,rmin, rmax, m)
% Rmin_Rmax 代表，输入的半径，是个圆环

%M2就是我们要的幅值，显示M2 ：imshow(uint8(M2/256));
%% Generation of PRND 这个语句的目的：就是生成伪随机的0,1比特水印串
% R = mychar2double(msg);
R=msg;
[M2,P2] = InputProc(Image);
%% initialization of dec variable
dec=inf;
%% 这个是整个文章的创新点，自适应嵌入。现在来看也不是什么新奇的方法
%% Search for optimal radius and maximum PSNR in interval 38-42 dB
while (dec>=42) || (dec<38)%希望的是 38<dec<42 太大怕鲁棒不太好，太小失真不好
    %L是载体图像。D,S是用来生成伪随机序列的种子,D,S固定的话，基本确定水印信息。
[ decibels_max, r_optim ] = My_max_psnr( Image,R,rmin,rmax,m);%半径优化

%% adaptation of the implementation factor to get a resonably degraded
% watermarked image (30 <= PSNR <= 42)
if decibels_max>=42
    m=m+(0.5*m);%强度优化
elseif decibels_max<=38
    m=m-(0.5*m);
end
dec=decibels_max;
end

%% vector implementation
E2 = Embed1(M2,R,m,r_optim);

%% return to spatial domain
W=OutputProc(E2,P2);
% W = ifft2(ifftshift(E2).*exp(1i*P2));
% W=uint8(W);
% %image adjustment to values 0 - 255
% W = imadjust(W,[single(min(min(W)))/255 ; single(max(max(W)))/255] , [single(min(min(L)))/255 ; single(max(max(L)))/255] );

%% connecting chromatic and luminance components (for color images)
% if broj_dim==3
% W=cat(3,W,L_cb,L_cr);
% W=ycbcr2rgb(W);
% end

%% file naming
% ime=image;
% ime(:,(max((size(ime)-3)):max(size(ime))))=[];
% imwrite(W, [ime '_' num2str(S) '_' num2str(m) '_' num2str(r_optim) '_' num2str(decibels_max, '%3.0f') '.bmp']);
% disp(['PSNR value is ', num2str(decibels_max)]);
% disp(['Implementation radius is ',num2str(r_optim)]);
% disp(['Implementation factor is ',num2str(m)]);

end